package com.demo.controller;


import com.demo.dto.Result;
import com.demo.dto.UserDTO;
import com.demo.dto.UserUpdateDTO;
import com.demo.entity.UserFollow;
import com.demo.service.UserFollowService;
import com.demo.service.UserService;
import com.demo.utils.UserHolder;
import com.demo.vo.UserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Zeng
 * @since 2022-03-23
 */
@Api(tags = "用户模块")
@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    UserFollowService userFollowService;

    @Resource
    UserService userService;

    @ApiOperation("关注-取消关注")
    @PostMapping("/follow")
    Result<UserFollow> follow(
            @RequestParam("target_user") Integer targetUserId
    ) {
        return userFollowService.follow(targetUserId);
    }

    @ApiOperation("修改用户信息（不包括密码、手机号、头像）")
    @PutMapping("/updateUserInfo")
    Result<UserVO> put(@RequestBody UserUpdateDTO userUpdateDTO, HttpServletRequest request) {
        return userService.put(userUpdateDTO, request);
    }

    @ApiOperation("用户简单信息")
    @GetMapping("/me/simple")
    Result<UserDTO> simpleData() {
        return Result.ok(UserHolder.getUser());
    }

    @ApiOperation("用户完整信息，用于编辑")
    @GetMapping("/me/detail")
    Result<UserVO> detailData() {
        return userService.detailData();
    }

    @ApiOperation("更新头像")
    @PostMapping("/upload-avatar")
    Result<UserVO> uploadAvatar(@RequestParam("file") MultipartFile image) {
        return userService.uploadAvatar(image);
    }
}

